//
//  YMJTabBarViewController.m
//  隐藏tabBar自定义
//
//  Created by dllo on 16/7/23.
//  Copyright © 2016年 YMJ. All rights reserved.
//

#import "YMJTabBarViewController.h"
#import "ViewController.h"
@interface YMJTabBarViewController ()
@property (nonatomic, strong) UIButton *selectedBtn;
@property (nonatomic, strong) ViewController *vc;
@property (nonatomic, strong) ViewController *vc1;
@property (nonatomic, strong) ViewController *vc2;
@property (nonatomic, strong) ViewController *vc3;
@end

@implementation YMJTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"%s",__FUNCTION__);
    NSLog(@"%@",self.view.subviews);
    // 删除现有tabBar
    CGRect rect = self.tabBar.frame;
    NSLog(@"rect是%@",NSStringFromCGRect(rect));
    [self.tabBar removeFromSuperview];
    // 测试添加自己的视图
    UIView *myView = [[UIView alloc] init];
    myView.frame = rect;
    myView.backgroundColor = [UIColor whiteColor];
    NSLog(@"self.view是%@",self.view);
    [self.view addSubview:myView];
    
    for (int i = 0; i < 5; i++) {
        UIButton *btn = [[UIButton alloc] init];
        NSString *imageName = [NSString stringWithFormat:@"0%d",i + 1];
        NSString *imageNameSel = [NSString stringWithFormat:@"%d%d",i + 1,i + 1];
        [btn setImage:[UIImage imageNamed:imageName] forState:0];
        [btn setImage:[UIImage imageNamed:imageNameSel] forState:UIControlStateSelected];
        CGFloat x = i * myView.frame.size.width / 4;
        btn.frame = CGRectMake(x, 0, myView.frame.size.width / 4, myView.frame.size.height);
        [myView addSubview:btn];
        btn.tag = i;
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        // 设置刚进入的按钮为选中
        if (i == 0) {
            btn.selected = YES;
            self.selectedBtn = btn;
        }
    }
    self.vc = [ViewController new];
    self.vc1 = [ViewController new];
    self.vc2 = [ViewController new];
    self.vc3 = [ViewController new];
    _vc.view.backgroundColor = [UIColor brownColor];
    _vc1.view.backgroundColor = [UIColor colorWithRed:0.545 green:0.329 blue:0.600 alpha:1.000];
    _vc2.view.backgroundColor = [UIColor colorWithRed:0.600 green:0.518 blue:0.231 alpha:1.000];
    _vc3.view.backgroundColor = [UIColor colorWithRed:0.123 green:0.600 blue:0.446 alpha:1.000];
    self.viewControllers = @[_vc,_vc1,_vc2,_vc3];
}

- (void)clickBtn:(UIButton *)btn {
    // 将之前选中的按钮设置为未选中
    self.selectedBtn.selected = NO;
    // 再将当前按钮设置成选中
    btn.selected = YES;
    // 最后把当前的按钮设置为selectedBtn
    self.selectedBtn = btn;
    // 跳转到相应视图控制器(通过selectIndex参数设置选中了那个控制器)
    self.selectedIndex = btn.tag;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
